package com.wnhz.work.yz.fragment.sell;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.SellAfterActivity;
import com.wnhz.work.yz.adapter.SellAfterAdapter;
import com.wnhz.work.yz.bean.SellAfterApplyBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/2/23 0023.
 */

public class SellAfterApplyForFragment extends Fragment {


    private View view;
    private ListView lv_sell_after;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = LayoutInflater.from(getContext()).inflate(R.layout.fragment_sell_after, null);
        initView();
        return view;
    }

    private void initView() {
        lv_sell_after = (ListView) view.findViewById(R.id.lv_sell_after);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        upLoad();

    }

    List<SellAfterApplyBean.InfoEntity> listBean = new ArrayList<>();
    SellAfterAdapter adapter;

    private void initData() {
        adapter = new SellAfterAdapter(context, listBean);
        lv_sell_after.setAdapter(adapter);
    }

    private SellAfterApplyBean bean;

    private void upLoad() {
        Map<String, Object> map = new Hashtable<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.ORDER_SALESSERVICELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                Log.e("----", result);
                bean = gson.fromJson(result, SellAfterApplyBean.class);
                listBean = bean.getInfo();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFinished() {
                super.onFinished();
                handler.sendEmptyMessage(1);
            }
        });

    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            initData();
            super.handleMessage(msg);
        }
    };

    @Override
    public void onDestroy() {
        if (handler != null)
            handler.removeCallbacks(null);
        super.onDestroy();

    }

    Context context;

    public void setContext(SellAfterActivity context) {
        this.context = context;
    }
}
